Below is a function that returns a DataTable using in C#. DataTables are derived from the namespace System.Data. We will also use a DataAdapter in this example. The DataAdapter Fills the DataTable from a SQL Database based on a SQL Query which is defined in a SQLCommand.
Database
C#
protected DataTable GetAllRecords() { SqlConnection sqlconn = new SqlConnection("Data Source=TutorialsPanel-DB\SQLEXPRESS;Initial Catalog=TutorialsPanel;Integrated Security=True;Pooling=False"); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand("Select * FROM ProgrammingLanguage Order by LanguageName"); DataTable dtLanguages = new DataTable(); adapter.SelectCommand = cmd; adapter.SelectCommand.Connection = sqlconn; adapter.Fill(dtLanguages); return dtLanguages; }
Comments